/**
 * Created by Administrator on 2018/11/22.
 */

console.log("Visit the point page!!!");

document.addEventListener('DOMContentLoaded', function () {
  console.log("Visit site: ", location.href);
  autoScroll();
});

var autoScroll = function () {
  // 当前 scroller 高度
  var incHeight = 0;
  // 当前获取页码数
  var pageNum = 0;
  // 旧页码总数，用于与最新页码总数比较，9次都一样即终止
  var oldPageCt = 0;
  // 终止次数
  var breakCount = 0;
  var inter = setInterval(function () {
    incHeight += 800;
    console.log("current height: ", incHeight, "current pageNum: ", pageNum);
    $("html, body").animate({scrollTop: incHeight}, 3100);
    var $page = $("div.imgpage");
    var currPage = $page.eq(pageNum);
    if (currPage) {
      var arr = $.map($(currPage).find("li.imgitem"), function (e) {
        // console.log(e);
        return $(e).attr("data-objurl");
      });
      // console.log(arr);
      if (arr.length > 0) {
        chrome.runtime.sendMessage({"images": arr}, function (response) {
          console.log("***", response)
        });
        pageNum += 1;
      }
    }
    if ($page.length == oldPageCt) {
      breakCount += 1;
    } else {
      breakCount = 0;
    }
    console.log("last page count: ", oldPageCt, "current page count: ", $page.length)
    oldPageCt = $page.length;
    if (breakCount >= 9) {
      clearInterval(inter);
    }
  }, 3000);
};



